<?php

namespace App\Controller\Admin\Rbac;

use App\Controller\Admin\BaseController;
use App\Service\Admin\AdminService;
use App\Validator\Admin\Rbac\ManagerValidator;
use Hyperf\Collection\Arr;
use Hyperf\Di\Annotation\Inject;

class ManagerController extends BaseController
{

    #[Inject]
    protected ManagerValidator $managerValidator;

    #[Inject]
    protected AdminService $adminService;

    public function pageList()
    {
        $params = $this->allParams();
        [$va, $page, $ps] = $this->managerValidator->pageList($params);
        $lists = $this->adminService->managerPageList($va, $page, $ps);
        return $this->success($lists);
    }

    public function detail()
    {
        $params = $this->allParams();
        $va = $this->managerValidator->detail($params);
        $detail = $this->adminService->adminBaseInfo($va['id']);
        if (!$detail) {
            return $this->fail('账号不存在');
        }
        $detail = Arr::except($detail, ['login_pwd']);
        return $this->success([
            'info' => $detail,
        ]);
    }

    public function add()
    {
        $params = $this->allParams();
        $va = $this->managerValidator->add($params);
        [$r, $msg] = $this->adminService->addAdmin($va, $this->getAdmin());
        if ($r) {
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    public function update()
    {
        $params = $this->allParams();
        $va = $this->managerValidator->update($params);
        [$r, $msg] = $this->adminService->updateAdminBase($va['id'], $va, $this->getAdmin());
        if ($r) {
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    public function del()
    {
        $params = $this->allParams();
        $va = $this->managerValidator->del($params);
        [$r, $msg] = $this->adminService->delAdmin($va['ids']);
        if ($r){
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    public function updateLoginPwd()
    {
        $params = $this->allParams();
        $va = $this->managerValidator->updateLoginPwd($params);
        [$r, $msg] = $this->adminService->updateAdminLoginPwd($va['id'], $va['login_pwd']);
        if ($r){
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    public function updateIsRoot()
    {
        $params = $this->allParams();
        $va = $this->managerValidator->updateIsRoot($params);
        [$r, $msg] = $this->adminService->updateAdminRoot($va['id'], $va['is_root']);
        if ($r){
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    public function updateSelfLoginPwd()
    {
        $params = $this->allParams();
        $va = $this->managerValidator->updateSelfLoginPwd($params);
        [$r, $msg] = $this->adminService->updateAdminLoginPwd($this->getAdminId(), $va['login_pwd']);
        if ($r){
            return $this->success(null, $msg);
        }
        return $this->fail($msg);
    }

    // 获取当前登录管理员账号信息
    public function currManagerInfo()
    {
        $detail = $this->getAdmin();
        if (!$detail) {
            return $this->fail('账号不存在');
        }
        $detail = Arr::except($detail, ['login_pwd']);
        $resp = [
            'info' => $detail,
        ];
        // 返回路由权限
        $getRoute = $this->request->input('with_route');
        if (!empty($getRoute)){
            $resp['power'] = $this->adminService->getManagerRoutes($detail);
        }
        return $this->success($resp);
    }
}